package com.magupe.im.web.db.service.h2;

import com.magupe.im.web.db.service.impl.DataServiceImpl;
import com.magupe.im.web.protocol.request.GroupMessageRequest;
import com.magupe.im.web.protocol.request.MessageRequest;

import java.util.List;

public class H2DataService extends DataServiceImpl {

    private final H2DataSource dataSource;

    public H2DataService(){
        this.dataSource = new H2DataSource();
        initTables();
    }

    public void initTables(){
        String table1 = "create table if not exists os_message" +
                        "(" +
                            "id varchar(32) primary key, " +
                            "app_id varchar(32), " +
                            "from_id varchar(32), " +
                            "receiver_id varchar(32), " +
                            "message varchar(255), " +
                            "create_time varchar(13)" +
                        ")";
        String table2 = "create table if not exists os_group_message" +
                        "(" +
                            "id varchar(32) primary key, " +
                            "app_id varchar(32), " +
                            "from_id varchar(32), " +
                            "group_id varchar(32), " +
                            "message varchar(255), " +
                            "create_time varchar(13)" +
                        ")";
        dataSource.execute(table1);
        dataSource.execute(table2);
    }

    @Override
    public void saveMessage(MessageRequest request) {
        String sql = "insert into os_message(id, app_id, from_id, receiver_id, message, create_time) " +
                "values('" +
                    request.getId() + "', '" +
                    request.getAppId() + "', '" +
                    request.getFromId() + "', '" +
                    request.getReceiverId()+"', '" +
                    request.getMessage() + "', '" +
                    System.currentTimeMillis() + "'" +
                ")";
        logger.info("data service. method saveMessage: {}", sql);
        dataSource.executeUpdate(sql);
    }

    @Override
    public void saveGroupMessage(GroupMessageRequest request) {
        String sql = "insert into os_group_message(id, app_id, from_id, receiver_id, message, create_time) " +
                "values('" +
                    request.getId() + "', '" +
                    request.getAppId() + "', '" +
                    request.getFromId() + "', '" +
                    request.getGroupId()+"', '" +
                    request.getMessage() + "', '" +
                    System.currentTimeMillis() + "'" +
                ")";
        logger.info("data service. method saveGroupMessage: {}", sql);
        dataSource.executeUpdate(sql);
    }

    @Override
    public List<MessageRequest> getMessages(String appId, String receiverId){
        String sql = "select * from os_message where app_id = '" + appId + "' and receiver_id = '" + receiverId + "' order by create_time asc";
        List<MessageRequest> messages = dataSource.getResultSetWithCollection(MessageRequest.class, sql);
        logger.info("data service. method getMessages: {}", sql);
        logger.info("data service. total: {}", null != messages ? messages.size() : 0);
        return messages;
    }

    @Override
    public List<GroupMessageRequest> getGroupMessages(String appId, String groupId){
        String sql = "select * from os_group_message where app_id = '" + appId + "' and group_id = '" + groupId + "'";
        List<GroupMessageRequest> groupMessages = dataSource.getResultSetWithCollection(GroupMessageRequest.class, sql);
        logger.info("data service. method getGroupMessages: {}", sql);
        logger.info("data service. total: {}", null != groupMessages ? groupMessages.size() : 0);
        return groupMessages;
    }

    @Override
    public void deleteMessages(String appId, String id, String fromId) {
        String sql = "delete from os_message where app_id = '" + appId + "' and id = '" + id + "' and from_id = '" + fromId + "'";
        logger.info("data service. method deleteMessages: {}", sql);
        dataSource.executeUpdate(sql);
    }

    @Override
    public void deleteGroupMessages(String appId, String id, String fromId) {
        String sql = "delete from os_group_message where app_id = '" + appId + "' and id = '" + id + "' and from_id = '" + fromId + "'";
        logger.info("data service. method deleteMessages: {}", sql);
        dataSource.executeUpdate(sql);
    }
}
